const { departModel } = require("../models");
const translateDataToTree = require("../utils/arrayToTree");
exports.createDepart = async (req, res, next) => {
  try {
    const addOneDepart = new departModel(req.body);
    const ret = await addOneDepart.save();
    res.status(200).json({
      data: ret,
    });
  } catch (err) {
    next(err);
  }
};
exports.getDepart = async (req, res, next) => {
  try {
    const allDepartsList = await departModel.find();
    // 根据pid 组织树结构
    const result = translateDataToTree(
      JSON.parse(JSON.stringify(allDepartsList))
    );
    res.status(200).json(result);
  } catch (err) {
    next(err);
  }
};
exports.deleteDepart = async (req, res, next) => {
  try {
    const ret = await departModel.deleteMany({ _id: { $in: req.body } });
    // const ret = await departModel.deleteMany({})
    res.status(200).json({
      data: ret,
    });
  } catch (err) {
    next(err);
  }
};
